import javax.crypto.spec.DESedeKeySpec;
import java.io.*;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-29
 * Time: 20:35
 */
//普通文件的复制
public class TestDemo2 {
    public static void main(String[] args) {
        System.out.println("请输入要复制的文件的路径");
        Scanner scanner=new Scanner(System.in);
        String src=scanner.next();
        System.out.println("请输入目的地路径");
        String des=scanner.next();
        File srcFile=new File(src);
        if(!srcFile.isFile()){
            System.out.println("源文件路径错误");
            return;
        }
        File desFile=new File(des);
        copyAndStick(srcFile,desFile);
    }


    //复制粘贴
    public static void copyAndStick(File srcFile,File desFile){
        try (InputStream inputStream=new FileInputStream(srcFile)){
            //写文件时文件不存在就直接创建,文件追加就加一个参数
            try(OutputStream outputStream=new FileOutputStream(desFile,true)){
                while(true){
                    byte [] buffer=new byte[1024];
                    int len=inputStream.read(buffer,0, buffer.length);
                    //读到文件末尾
                    if(len==-1){
                        break;
                    }
                    //从数组的offset位置开始读len长度然后写
                    outputStream.write(buffer,0,len);
                }
                System.out.println("复制成功");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
}
